home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / audio / rock / clipsound.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.3 KB  |  208 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    clipsound - 
  19.  *        display and extract a sub sample from a aiff file  
  20.  *
  21.  *                Paul Haeberli - 1991 
  22.  */ 
  23. #include "stdio.h"
  24. #include "gl.h"
  25. #include "device.h"
  26. #include "sample.h"
  27.  
  28. float fgetmousex();
  29.  
  30. printdoc()
  31. {
  32.     printf("\nuse the left mouse button to select a range of the sample\n");
  33.     printf("click the middle mouse button to play the selection\n");
  34.     printf("use the menu to zoom in on the stuff you've selected\n");
  35.     printf("or to write out the selected sample\n\n");
  36. }
  37.  
  38. int pos1, pos2;
  39.  
  40. main(argc,argv)
  41. int argc;
  42. char **argv;
  43. {
  44.     sample *s, *subsamp;
  45.     short val;
  46.     int i, opos1, menu;
  47.     float fdelta;
  48.  
  49.     subsamp = 0;
  50.     if(argc<2) {
  51.     fprintf(stderr,"usage: clipsample in.a\n");
  52.     exit(1);
  53.     }
  54.     printdoc();
  55.     winopen("aiff");
  56.     RGBmode();
  57.     gconfig();
  58.  
  59.     cpack(0x808080);
  60.     clear();
  61.  
  62.     s = readsample(argv[1]);
  63.     expandsample(s);
  64.     printsample(s);
  65.     drawit(s);
  66.     qdevice(LEFTMOUSE);
  67.     qdevice(MIDDLEMOUSE);
  68.     qdevice(RIGHTMOUSE);
  69.     qdevice(ESCKEY);
  70.     qdevice(UPARROWKEY);
  71.     qdevice(DOWNARROWKEY);
  72.     menu = defpup("clipsound %t|zoom into selection|write sample");
  73.     fdelta = 16000;
  74.     while(1) {
  75.     switch(qread(&val)) {
  76.         case ESCKEY:
  77.         exit(1);
  78.         break;
  79.         case REDRAW:
  80.         drawit(s);
  81.         break;
  82.         case LEFTMOUSE:
  83.         if(val) {
  84.             if(myshiftdown()) {
  85.             while(getbutton(LEFTMOUSE)) {
  86.                 pos1 = s->nsamples*fgetmousex()/s->nchannels;
  87.                 pos1 = pos1-fdelta/2;
  88.                 pos2 = pos1+fdelta/2;
  89.                 drawselection(s,pos1,pos2);
  90.                 if(subsamp)
  91.                 freesample(subsamp);
  92.                 subsamp = subsample(s,2*pos1,2*pos2);
  93.                 playsample(subsamp);
  94.                 flushsample();
  95.             }
  96.             } else {
  97.             pos1 = s->nsamples*fgetmousex()/s->nchannels;
  98.             while(getbutton(LEFTMOUSE)) {
  99.                 pos2 = s->nsamples*fgetmousex()/s->nchannels;
  100.                 drawselection(s,pos1,pos2);
  101.                 sginap(5);
  102.             }
  103.             printf("%d %d\n",pos1,pos2);
  104.             if(subsamp)
  105.                 freesample(subsamp);
  106.             if(pos2 == pos1)
  107.                 pos2 = pos1+1;
  108.             subsamp = subsample(s,2*pos1,2*pos2);
  109.             playsample(subsamp);
  110.             }
  111.         }
  112.         break;
  113.         case MIDDLEMOUSE:
  114.         if(val) {
  115.             if(subsamp)
  116.             playsample(subsamp);
  117.         }
  118.         break;
  119.         case RIGHTMOUSE:
  120.         if(val) {
  121.             switch(dopup(menu)) {
  122.             case 1:
  123.                 if(subsamp) {
  124.                 freesample(s);
  125.                 s = subsamp;
  126.                 subsamp = 0;
  127.                 pos1 = pos2 = 0;
  128.                 drawit(s);
  129.                 }
  130.                 break;
  131.             case 2:
  132.                 if(subsamp)
  133.                 writesample(subsamp,"clip.a");
  134.                 else
  135.                 writesample(s,"clip.a");
  136.                 printf("sample written to clip.a\n");
  137.                 break;
  138.             }
  139.         }
  140.         break;
  141.         case UPARROWKEY:
  142.         if(val) 
  143.             fdelta = fdelta*1.41;
  144.         break;
  145.         case DOWNARROWKEY:
  146.         if(val) 
  147.             fdelta = fdelta/1.41;
  148.         break;
  149.     }
  150.     }
  151. }
  152.  
  153. drawit(s)
  154. sample *s;
  155. {
  156.     reshapeviewport();
  157.     drawsamples(s);
  158.     drawselection(s,pos1,pos2);
  159. }
  160.  
  161. drawsamples(s)
  162. sample *s;
  163. {
  164.     int i, nframes;
  165.     short *sptr;
  166.     int ibuf[2];
  167.     int inc;
  168.  
  169.     nframes = s->nsamples/2.0;
  170.     inc = 1+(nframes/2000.0);
  171.     printf("inc is %d nframes is %d\n",inc,nframes);
  172.     ortho2(0.0,(float)nframes,-40000.0,40000.0);
  173.     cpack(0x808080);
  174.     clear();
  175.  
  176.     sptr = s->data;
  177.     for(i=0; i<nframes; i+=inc) {
  178.     cpack(0x0000ff);
  179.     move2i(i,0);
  180.     draw2i(i,sptr[0]);
  181.     cpack(0x00ff00);
  182.     move2i(i,0);
  183.     draw2i(i,sptr[1]);
  184.     sptr += 2*inc;
  185.     }
  186. }
  187.  
  188. drawselection(s,pos1,pos2)
  189. sample *s;
  190. int pos1,pos2;
  191. {
  192.     int nframes;
  193.  
  194.     nframes = s->nsamples/2.0;
  195.     cpack(0x808080);
  196.     rectfi(0,-40000,nframes,-40000+1000);
  197.     cpack(0x000000);
  198.     rectfi(pos1,-40000,pos2,-40000+1000);
  199. }
  200.  
  201. myshiftdown()
  202. {
  203.     if(getbutton(LEFTSHIFTKEY) || getbutton(RIGHTSHIFTKEY))
  204.     return 1;
  205.     else
  206.     return 0;
  207. }
  208.